package io.renren.modules.avanotes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.vo.input.SpecialSubjectAchivementEvaluationIUVo;
import io.renren.modules.avanotes.component.EbbinghausReviewContextComponent;
import io.renren.modules.avanotes.dao.SpecialSubjectAchivementEvaluationDao;
import io.renren.modules.avanotes.entity.ReviewScheduleEntity;
import io.renren.modules.avanotes.entity.SpecialSubjectAchivementEvaluationEntity;
import io.renren.modules.avanotes.entity.StudyScheduleEntity;
import io.renren.modules.avanotes.entity.Supermemo2SpecialReviewEntity;
import io.renren.modules.avanotes.service.ReviewScheduleService;
import io.renren.modules.avanotes.service.SpecialSubjectAchivmentEvaluationService;
import io.renren.modules.avanotes.service.StudyScheduleService;
import io.renren.modules.avanotes.service.Supermemo2SpecialReviewService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


//@Service("specialSubjectAchivmentEvaluationService")//默认只有一个实现，那就不写名称了直接使用默认的名称！
@Service
public class SpecialSubjectAchivementEvaluationServiceImpl extends ServiceImpl<SpecialSubjectAchivementEvaluationDao, SpecialSubjectAchivementEvaluationEntity> implements SpecialSubjectAchivmentEvaluationService {

    @Autowired
    EbbinghausReviewContextComponent ebbinghausReviewContextComponent;

    @Autowired
    StudyScheduleService studyScheduleService;

    @Autowired
    ReviewScheduleService reviewScheduleService;

    @Autowired
    Supermemo2SpecialReviewService supermemo2SpecialReviewService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecialSubjectAchivementEvaluationEntity> page = this.page(
                new Query<SpecialSubjectAchivementEvaluationEntity>().getPage(params),
                new QueryWrapper<SpecialSubjectAchivementEvaluationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     * NOTEME 并进入自动指定下一个复习间隔！（默认使用 SM2）
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insert(SpecialSubjectAchivementEvaluationIUVo vo) {
        SpecialSubjectAchivementEvaluationEntity entity = new SpecialSubjectAchivementEvaluationEntity();
        BeanUtils.copyProperties(vo, entity);
        //获取 int 格式的 日期 -- 改为计划的日期！NOTEME 所以要求计划必须当天进行评价！
        //Integer day = Integer.parseInt(DateUtils.format(new Date(), DateUtils.DATE_PATTERN_INT));
        //获取计划
        StudyScheduleEntity studyEntity = studyScheduleService.getById(vo.getStudyId());
        //entity.setDay(studyEntity.getDay());
        entity.setDay(Integer.parseInt(DateUtils.format(new Date(), DateUtils.DATE_PATTERN_INT)));
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        entity.setCreateUserId(userEntity.getUserId());
        //region 通过艾宾浩斯曲线生成（后来发现并不科学，不能根据每次复习的效果进行反馈，于是使用了SM2）

        //计划ID
//        List<ReviewScheduleEntity> list = ebbinghausReviewContextComponent.genReviewSchedulesByLevel(vo.getStudyId(), vo.getSpecialId(), studyEntity.getDay(), userEntity.getUserId(), vo.getEvaluationLevel());
//        reviewScheduleService.saveBatch(list);
//        this.save(entity);
//        return entity.getId();
        //endregion

        //region 使用SM2 生成！

        //由于是直接新增的，所以根据公式 I(1) =1,得到间隔=1
        int interval = 1;
        Supermemo2SpecialReviewEntity reviewEntity = new Supermemo2SpecialReviewEntity(vo.getStudyId(), vo.getSpecialId().longValue(), 0d, 0, 1, 1, interval, userEntity.getUserId());
        //保存计算中间值
        supermemo2SpecialReviewService.save(reviewEntity);
        //生成下一步计划
        ReviewScheduleEntity reviewScheduleEntity = new ReviewScheduleEntity(vo.getStudyId(), vo.getSpecialId(), userEntity.getUserId(), entity.getDay() + interval);
        //保存下一步复习计划
        reviewScheduleService.save(reviewScheduleEntity);
        //保存评价数据
        this.save(entity);
        return entity.getId();
        //endregion
    }

    @Override
    public Integer getCountByDateAndUserIdAndSpecialId(Integer date, Integer specialId, Long userId) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        int count = this.count(new QueryWrapper<SpecialSubjectAchivementEvaluationEntity>().eq(SpecialSubjectAchivementEvaluationEntity.CN_DAY, date)
                .eq(SpecialSubjectAchivementEvaluationEntity.CN_SPECIAL_ID, specialId).eq(SpecialSubjectAchivementEvaluationEntity.CN_CREATE_USER_ID, userEntity.getUserId()));
        return count;
    }

}